package com.lining.orangeservice.mapper;

import com.lining.orangeservice.entity.po.Permission;
import com.lining.orangeservice.entity.po.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PermissionMapper {

    /**
     * 根据用户ID查询权限列表
     */
    @Select("SELECT DISTINCT p.id, p.perm_name, p.perm_code, p.perm_type, p.parent_id, " +
            "p.path, p.component, p.icon, p.sort, p.status, p.create_time, p.update_time " +
            "FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.status = 1 " +
            "ORDER BY p.sort ASC")
    List<Permission> selectPermissionsByUserId(@Param("userId") Long userId);

    /**
     * 查询菜单权限（perm_type = 1）
     */
    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, " +
            "path, component, icon, sort, status, create_time, update_time " +
            "FROM sys_permission WHERE perm_type = 1 AND status = 1 " +
            "ORDER BY sort ASC")
    List<Permission> selectMenuPermissions();

    /**
     * 查询所有菜单权限（权限类型为1的权限）
     *
     * @return 权限列表
     */
    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status, create_time, update_time " +
            "FROM sys_permission " +
            "WHERE perm_type = 1 " +
            "ORDER BY sort ASC")
    List<Permission> selectAllMenus();

    /**
     * 查询所有权限（包括菜单、按钮、接口等所有类型）
     *
     * @return 权限列表
     */
    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status, create_time, update_time " +
            "FROM sys_permission " +
            "ORDER BY perm_type ASC, sort ASC")
    List<Permission> selectAllPermissions();

    /**
     * 根据父ID查询菜单
     *
     * @param parentId 父权限ID
     * @return 权限列表
     */
    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status, create_time, update_time " +
            "FROM sys_permission " +
            "WHERE parent_id = #{parentId} AND perm_type = 1 " +
            "ORDER BY sort ASC")
    List<Permission> selectMenusByParentId(Long parentId);

    /**
     * 根据用户ID查询菜单权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select("SELECT DISTINCT p.id, p.perm_name, p.perm_code, p.perm_type, p.parent_id, p.path, p.component, p.icon, p.sort, p.status, p.create_time, p.update_time " +
            "FROM sys_permission p " +
            "INNER JOIN sys_role_permission rp ON p.id = rp.permission_id " +
            "INNER JOIN sys_user_role ur ON rp.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND p.perm_type = 1 AND p.status = 1 " +
            "ORDER BY p.sort ASC")
    List<Permission> selectMenusByUserId(Long userId);

    /**
     * 根据ID查询权限
     */
    @Select("SELECT * FROM sys_permission WHERE id = #{id}")
    Permission selectById(@Param("id") Long id);

    @Select("SELECT * FROM sys_permission WHERE id IN (SELECT permission_id FROM sys_role_permission WHERE role_id = #{roleId})")
    List<Permission> selectByRoleId(Long roleId);


    /**
     * 删除角色权限关系
     */
    @Delete({
            "<script>",
            "DELETE FROM role_permission WHERE role_id = #{roleId} AND permission_id IN ",
            "<foreach collection='permissionIds' item='permissionId' open='(' separator=',' close=')'>",
            "#{permissionId}",
            "</foreach>",
            "</script>"
    })
    void deleteRolePermissions(@Param("roleId") Long roleId, @Param("permissionIds") List<Long> permissionIds);


    /**
     * 根据ID查询权限
     */
    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status, create_time, update_time FROM sys_permission WHERE id = #{id}")
    Permission selectPermissionById(@Param("id") Long id);

    @Select("SELECT COUNT(*) FROM sys_permission WHERE perm_name = #{name}")
    int countByName(String name);

    @Insert("INSERT INTO sys_permission (perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status) VALUES (#{permName}, #{permCode}, #{permType}, #{parentId}, #{path}, #{component}, #{icon}, #{sort}, #{status})")
    void insert(Permission permission);

    @Update("UPDATE sys_permission SET perm_name = #{permName}, perm_code = #{permCode}, perm_type = #{permType}, parent_id = #{parentId}, path = #{path}, component = #{component}, icon = #{icon}, sort = #{sort}, status = #{status} WHERE id = #{id}")
    void update(Permission permission);

    @Delete("DELETE FROM sys_permission WHERE id = #{id}")
    void delete(Long id);

    @Select("SELECT id, perm_name, perm_code, perm_type, parent_id, path, component, icon, sort, status, create_time, update_time FROM sys_permission")
    List<Permission> selectAll();
}